﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class login : System.Web.UI.Page
{
    //inject IEmployeeBLL
    private IEmployeeBLL empBLL = Factory<IEmployeeBLL>.CreateInstance();

    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(login).Name);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Inits();
        }
    }
    private void Inits()
    {
        txtUser.Attributes.Add("onKeyPress", "return submitenter(event)");
        txtPass.Attributes.Add("onKeyPress", "return submitenter(event)");
        checker.Attributes.Add("onKeyPress", "return submitenter(event)");
        txtUser.Focus();
    }

    /// <summary>
    /// Check login
    /// </summary>
    /// <returns>bool</returns>
    private void CheckLogin()
    {
        try
        {
            //inits BO and set properties
            Employee emp = new Employee();
            emp.UserName = txtUser.Text.Trim();
            emp.Password = txtPass.Text.Trim();
                        emp = empBLL.CheckLogin(emp);
            //login success
            if (!emp.Id.Equals(Guid.Empty))
            {
                //Register to session 
                Session["Employee"] = emp;
                //redirect to Employee page
                Response.Redirect(Constants.EMPLOYEE_SHOW,false);
            }
            //login fail
            else
            {
                lblMsg.Text = "Đăng nhập thất bại !";
                txtUser.Focus();
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //System.Threading.Thread.Sleep(3000);
        CheckLogin();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.Page_Load(null, null);
    }
}